﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model_SSResourceManager;
using Presenter_SSResourceManager;

namespace WebView_SSResourceManager
{
    public partial class EditQueue : System.Web.UI.Page, IViewEditQueue
    {
        private PresenterEditQueue _editQueuePresenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            _editQueuePresenter = new PresenterEditQueue(this);

            if (!IsPostBack)
                try
                {
                    var query = HttpContext.Current.Request.Url.Query.Where(symbol => symbol != '?');
                    Guid queueId = Guid.Parse(new string(query.ToArray()));
                    Session["TempQueueId"] = queueId;
                    _editQueuePresenter.FillFieldToEdit(queueId);
                }
                catch (Exception)
                {
                    Response.Redirect("~/");
                }
        }

        public void FillPageFields(Queue resource)
        {
            Session["TempUserId"] = resource.IdUser;
            Session["TempResourceId"] = resource.IdResource;
            Session["TempMessageTime"] = resource.MessageTime;
            lbl_ResourceName.Text = _editQueuePresenter.GetResourceName(resource.IdResource);
            lbl_UserName.Text = _editQueuePresenter.GetUserName(resource.IdUser);
            txtB_EndTime.Text = resource.FinishTime.ToShortTimeString();
            txtB_StartTime.Text = resource.StartTime.ToShortTimeString();
        }

        public Queue GatherPageFieldsInformation()
        {
            return new Queue()
                       {
                           Id = (Guid)Session["TempQueueId"],
                           IdUser = (Guid)Session["TempUserId"],
                           IdResource = (Guid)Session["TempResourceId"],
                           MessageTime = (DateTime?)Session["TempMessageTime"],
                           StartTime = DateTime.Parse(txtB_StartTime.Text),
                           FinishTime = DateTime.Parse(txtB_EndTime.Text)
                       };
        }

        protected void Submit_OnClick(object sender, EventArgs e)
        {
            _editQueuePresenter.Update();
        }
    }
}